<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      ref="formRef"
      :model="formData"
      label-width="0"
    >
      <el-row style=" border: 1px solid #999;" type="flex" class="row-bg" justify="center" align="top">
        <el-col :span="12" class="no-border">
          <el-input v-if="formLoading" disabled type="textarea" v-model="formSubResult.dataQueryDO.autidContent" :placeholder="copyContent" :autosize="{ minRows: 20, maxRows: 24}" />
        </el-col>
        <el-col :span="12">
          <el-row type="flex" class="row-bg" justify="center" style="padding: 10px">
            <el-col :span="24">智能审核<el-divider style="margin: 5px 0"/></el-col>
            <el-col :span="24">
              <el-row type="flex" class="row-bg" align="bottom">
                <h3 style="margin-right: 15px">{{formSubResult?.dataQueryDO?.queryName}}</h3>
                <span style="font-size: 14px">{{formSubResult?.dataQueryDO?.validGender}} | {{formSubResult?.dataQueryDO?.validAge}} 岁</span>
                <span style="font-size: 14px; margin-left: 20px">
                  {{formSubResult?.dataQueryDO?.queryIdCard}}
                  <Icon @click="copyIdCardMethod" icon="ep:document-copy" class="ml-5px w-full h-full text-[#1E90FF]" />
                </span>
              </el-row>
              <el-row type="flex" class="row-bg mt-10px" align="bottom">
                <p v-if="formSubResult?.dataQueryDO?.validAddress" style="margin-right: 15px; font-size: 14px">{{formSubResult?.dataQueryDO?.validAddress}}</p>
                <el-tag v-if="formSubResult?.dataQueryDO?.validAddressType" size="small">{{formSubResult?.dataQueryDO?.validAddressType}}</el-tag>
              </el-row>
              <el-row type="flex" class="row-bg" justify="center" style="border: 1px solid #bfcbd9; margin-top: 10px">
                <el-col :span="6" style="text-align: center; padding: 5px; font-size: 13px; background: #f0f2f5">撸子分析</el-col>
                <el-col :span="6" style="text-align: center; padding: 5px; font-size: 13px; background: #f0f2f5">证件二要素</el-col>
                <el-col :span="6" style="text-align: center; padding: 5px; font-size: 13px; background: #f0f2f5">探查结果</el-col>
                <el-col :span="6" style="text-align: center; padding: 5px; font-size: 13px; background: #f0f2f5">履约笔数</el-col>
                <el-col :span="6" style="text-align: center; padding: 5px; font-size: 13px">
                  <p v-if="formSubResult?.dataQueryDO?.riskType === 1" style="color: #2bb673; font-size: 13px">安全客户</p>
                  <p v-else style="font-size: 13px">
                    <span style="color: red;">{{formSubResult?.dataQueryDO?.riskType === 2 ? '撸子' : '疑似撸'}}</span>
                    <el-popover
                      placement="bottom"
                      width="700"
                      trigger="click">
                      <el-table :data="lzList" style="height: 280px">
                        <el-table-column label="姓名" align="center" prop="name" />
                        <el-table-column label="手机号" align="center" prop="mobile" />
                        <el-table-column label="身份证" align="center" prop="idCard" min-width="160"/>
                        <el-table-column label="用户类型" align="center" prop="type" />
                        <el-table-column label="查询次数" align="center" prop="queryCount" />
                      </el-table>
                      <template #reference>
                        <span>(<span style="cursor: pointer; color: #2d8cf0">{{formSubResult?.dataQueryDO?.riskTypeFinanceIds.length}}</span>)</span>
                      </template>
                    </el-popover>
                  </p>
                </el-col>
                <el-col :span="6" style="text-align: center; padding: 5px; font-size: 13px">
                  <el-tag type="info" v-if="formSubResult?.dataQueryDO?.vaildResult === 0">未校验</el-tag>
                  <el-tag type="success" v-if="formSubResult?.dataQueryDO?.vaildResult === 1">一致</el-tag>
                  <el-tag type="danger" v-else>不一致</el-tag>
                </el-col>
                <el-col :span="6" style="text-align: center; padding: 5px; font-size: 13px">
                  <el-tag type="danger" v-if="formSubResult?.dataQueryTanzcDO?.resultCode === '1'">逾期未还款</el-tag>
                  <el-tag type="success" v-if="formSubResult?.dataQueryTanzcDO?.resultCode === '2'">正常履约</el-tag>
                  <el-tag type="warning" v-else>未知</el-tag>
                </el-col>
                <el-col :span="6" style="text-align: center; padding: 5px; font-size: 13px">
                  <el-tag v-if="formSubResult?.dataQueryTanzcDO?.countPerformance" type="success">{{formSubResult?.dataQueryTanzcDO?.countPerformance}}</el-tag>
                  <el-tag v-else type="info">暂无</el-tag>
                </el-col>
                <el-col :span="6" style="text-align: center; padding: 5px; font-size: 13px; background: #f0f2f5">最大履约金额</el-col>
                <el-col :span="6" style="text-align: center; padding: 5px; font-size: 13px; background: #f0f2f5">最近履约时间</el-col>
                <el-col :span="6" style="text-align: center; padding: 5px; font-size: 13px; background: #f0f2f5">最大逾期金额</el-col>
                <el-col :span="6" style="text-align: center; padding: 5px; font-size: 13px; background: #f0f2f5">逾期机构数</el-col>
                <el-col :span="6" style="text-align: center; padding: 5px; font-size: 13px">
                  <el-tag v-if="formSubResult?.dataQueryTanzcDO?.maxPerformanceAmt" type="success">{{formSubResult?.dataQueryTanzcDO?.maxPerformanceAmt}}</el-tag>
                  <el-tag v-else type="info">暂无</el-tag>
                </el-col>
                <el-col :span="6" style="text-align: center; padding: 5px; font-size: 13px">
                  <el-tag v-if="formSubResult?.dataQueryTanzcDO?.latestPerformanceTime" type="success">{{formSubResult?.dataQueryTanzcDO?.latestPerformanceTime}}</el-tag>
                  <el-tag v-else type="info">暂无</el-tag>
                </el-col>
                <el-col :span="6" style="text-align: center; padding: 5px; font-size: 13px">
                  <el-tag v-if="formSubResult?.dataQueryTanzcDO?.maxOverdueAmt" type="success">{{formSubResult?.dataQueryTanzcDO?.maxOverdueAmt}}</el-tag>
                  <el-tag v-else type="info">暂无</el-tag>
                </el-col>
                <el-col :span="6" style="text-align: center; padding: 5px; font-size: 13px">
                  <el-tag v-if="formSubResult?.dataQueryTanzcDO?.currentlyOverdue" type="success">{{formSubResult?.dataQueryTanzcDO?.currentlyOverdue}}</el-tag>
                  <el-tag v-else type="info">暂无</el-tag>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
          <el-row type="flex" class="row-bg" justify="left" style="padding: 10px">
            <el-col :span="24">深度查询<el-divider style="margin: 5px 0"/></el-col>
            <el-col :span="24">
              <el-checkbox-group v-model="formSelApis">
                <el-checkbox v-for="api in auditApis" :key="api.id" :value="api.code" :label="api.name + ' ( ' + api.price + ' 元 )'"/>
              </el-checkbox-group>
            </el-col>
            <el-col :span="24" style="margin-top: 10px">
              <el-button size="small" type="primary" @click="submitQuery">查 询</el-button>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
    </el-form>
    <el-row style="margin-top: 10px" type="flex" class="row-bg" justify="center" align="center" >
      <el-button @click="submitForm(true)" type="primary">刷新结果（免费）</el-button>
      <el-button @click="returnAuditPage">返 回</el-button>
    </el-row>
  </ContentWrap>
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import { DataQueryApi, DataQuery } from '@/api/business/dataquery'
import { ApiTypeInfoApi, ApiTypeInfo } from '@/api/business/apitypeinfo'
import { TfFinanceApi, TfFinance } from '@/api/business/tffinance'
import {ContentWrap} from "@/components/ContentWrap";
import {Icon} from "@/components/Icon";
import {useTagsView} from "@/hooks/web/useTagsView";
const { closeCurrent } = useTagsView()

/** 智能比对 列表 */
defineOptions({ name: 'InfoAuditDetail' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const submitQuery = async () => {
  // 提交请求
  try {
    let id = await DataQueryApi.createDataQueryAuditDetail({id: formSubResult.value.dataQueryDO?.id, queryType: formSelApis.value})
    message.success("查询成功")
    closeCurrent(this)
    router.push({
      name: 'DataQueryDetail',
      query: { id, location: 'InfoAudit' }
    })
  } finally {
  }
}

/** 加入黑名单 */
const returnAuditPage = async () => {
  closeCurrent(this)
  router.push({
    name: 'InfoAudit'
  })
}

const lzList = ref() // 搜索的表单
/** 获取撸子 */
const getLz = async () => {
  let ids = formSubResult.value?.dataQueryDO?.riskTypeFinanceIds;
  if (ids && ids.length > 0) {
    let data = {ids: formSubResult.value?.dataQueryDO?.riskTypeFinanceIds}
    lzList.value = await TfFinanceApi.getTfFinancesByIds(data)
  }
}


const zndbApiPrice = ref()
/** 查询列表 */
const getZndbApi = async () => {
  zndbApiPrice.value = await ApiTypeInfoApi.getApiZnshPrice()
}

let formSelApis = ref([])
const auditApis = ref()
/** 查询列表 */
const getAuditApis = async () => {
  auditApis.value = await ApiTypeInfoApi.getApiTypeInfoAll(2)
}

let copyContent = ref('【申请额度】：\n' +
  '【联系方式】：\n' +
  '【姓名】：\n' +
  '【身份证号】：\n' +
  '【合作财务】：\n' +
  '【现居住地】：\n' +
  '【户籍所在地】：\n' +
  '【芝麻分】：\n' +
  '【能否接受腾讯会议】：')

const copyIdCardMethod = async () => {
  navigator.clipboard.writeText(formSubResult.value?.dataQueryDO?.queryIdCard)
  message.success('复制成功')
}

const formData = ref({
  content: undefined,
})
const formRef = ref() // 表单 Ref

const formSubResult = ref();

let formLoading = ref(false);

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async (show:boolean) => {
  // 提交请求
  try {
    // 从路由参数中获取 ID
    formSubResult.value = await DataQueryApi.getDataQueryAudit(route.query.id as any)
    if (show){
      message.success("刷新成功")
    }
    await getLz()
  } finally {
    formLoading.value = true;
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    content: undefined,
  }
  formRef.value?.resetFields()
}

const route = useRoute() // 路由
const router = useRouter() // 路由

/** 初始化 **/
onMounted(() => {
  // 如果 ID 不存在，显示错误提示并关闭页面
  if (!route.query.id) {
    message.error('智能审核记录 ID 不存在，无法查看详情')
    // 关闭当前路由，返回到列表页面
    router.push({ name: 'InfoAudit' })
    return
  }

  submitForm(false)
  getAuditApis()
})
</script>

<style scoped>
.no-border>>>.el-textarea__inner {
  height: 100%;
  border: 0; /* 去掉边框 */
  outline: none; /* 去掉聚焦时的外边框 */
  resize: none;
}
</style>
